using System;
using ClimbTracker.BusinessLogic.Entities;
using NHibernate;

namespace ClimbTracker.BusinessLogic.Dao
{
    /// <summary>
    /// Summary description for RouteDao.
    /// </summary>
    public class RouteDao : BaseDao
    {
        public RouteDao()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Retrieve a Route object from the database by its ID
        /// </summary>
        /// <param name="routeID"></param>
        /// <returns></returns>
        public Route retrieveRouteByID(int routeID)
        {
            Route route = null;
            try
            {
                route = (Route)currentSession.Load(typeof(Route), routeID);
            }
            catch (HibernateException ex)
            {
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }

            return route;

        }

        /// <summary>
        /// Create a new route entry in the database from the given Route object
        /// </summary>
        /// <param name="route"></param>
        public void createRoute(Route route)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Save(route);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Delete a the database entry of the given Route object
        /// </summary>
        /// <param name="route"></param>
        public void deleteRoute(Route route)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Delete(route);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Update the database entry of the given Route object
        /// </summary>
        /// <param name="route"></param>
        public void updateRoute(Route route)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Update(route);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }



    }
}
